import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;


/**
 * @author zfChen
 * @create 2022/7/6 10:31
 */
@Slf4j
public class NettyFuture {
    public static void main(String[] args) {
        // 1、线程池
        NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        log.info("main start");

        // 2、提交任务
        Future<Integer> future = eventLoopGroup.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                log.info("执行计算");
                Thread.sleep(1000);
                return 50;
            }
        });

        // 3、异步接收结果
        future.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                log.info("结果是：{}",future.getNow());
                eventLoopGroup.shutdownGracefully();
            }
        });

        // 主线程结束
        log.info("main end");
    }
}
